//手机号码正则表达式
var phoneReg = /^1[345789][0-9]{9}$/;
//密码正则表达式
var pwdReg = /^.{6,16}$/;
var isPhoneRight = false;
var isPwdRight = false;
var eye = document.getElementById("eye");
var pwd = document.getElementById("pwd");
var phone = document.getElementById("phone");
var loginBtn = document.getElementById("loginBtn");
var pwderror = document.getElementById("pwderror");
var flag = 0;
var i = 0;
//向用户提示手机号码格式是否正确
phone.onblur = function(){
    var phoneValue = phone.value;
    if(!phoneReg.test(phoneValue)){
        phoneerror.innerHTML = "<img src='images/error.png'/>";
        isPhoneRight = false;
    }else{
        phoneerror.innerHTML = "<img src='images/green.png'/>";
        isPhoneRight = true;
    }
    checkAll();
};

//向用户提示密码的格式是否正确
pwd.onkeyup = function(){
    //算法 --- 把用户输入的值变成隐藏模式
    // var password = $("#pwd").val();
    // if(password == null || password == ""){
    //     return;
    // }
    //
    // userinputPwd = userinputPwd+password;
    // ++i;
    //
    // var dot = "";
    // for(var a = 0;a<i;a++){
    //     dot +="*";
    // }
    // $("#pwd").val(dot);
    var pwdValue = pwd.value;
    if(!pwdReg.test(pwdValue)){
        pwderror.innerHTML = "<img src='images/error.png'/>";

        isPwdRight = false;
    }else{
        pwderror.innerHTML = "<img src='images/green.png'/>";
        isPwdRight = true;

    }
    checkAll();
};

function checkAll(){
    if(isPhoneRight&&isPwdRight){
        loginBtn.className = "loginBtn loginBtn_active";
    }else{
        loginBtn.className = "loginBtn";
    }
}

eye.onclick = function(){
    //方法一 ：  pwd.type="";
    //当用户点击眼睛的时候，获取密码框中的密码
    var password = $("#pwd").val();
    if(password == null || password == ""){
        alert("请检查用户密码");
        return;
    }else if(flag == 0){
        pwd.type="";
        flag = 1;
    }else if(flag == 1){
        pwd.type="password";
        flag = 0;
    }
};

loginBtn.onclick = function(){
    if(this.className!="loginBtn"){
        loginBtn.innerHTML = "登录中";
        $.post("http://test.hfjava.com/shop/userlogin",{"phone":phone.value,"pwd":pwd.value},
            function(data){
                loginBtn.innerHTML = "登录";
                if(data=="false"){
                    pwderror.innerHTML = "您的用户名或密码有误，请您重试！";

                }else{

                    //alert(phone.value);
                    //alert(pwd.value);
                    location.href = "index.html?phone="+phone.value;
                }
            })
    }
};
